package au.edu.unsw.infs3634.gamifiedlearning;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.adapter.JoinAdapter;

public class AppointMeeting extends AppCompatActivity {
    private DatePickerDialog dateDialog;
    private TimePickerDialog timeDialog;
    private int year, monthOfYear, dayOfMonth, hourOfDay, minute;
    String strStartTime = "";
    String strEndTime = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appoint_meeting);

        // Back button
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        setTitle("预约会议");


        TextView startTime = findViewById(R.id.startTime);
        TextView endTime = findViewById(R.id.endTime);

        // 通过Calendar对象来获取年、月、日、时、分的信息
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(calendar.YEAR);
        monthOfYear = calendar.get(calendar.MONTH);
        dayOfMonth = calendar.get(calendar.DAY_OF_MONTH);
        hourOfDay = calendar.get(calendar.HOUR_OF_DAY);
        minute = calendar.get(calendar.MINUTE);



        // 设置开始时间对话框
        startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                 * 实例化DatePickerDialog
                 */
                dateDialog = new DatePickerDialog(view.getContext(), new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
                        // 把获取的日期显示在文本框内，月份从0开始计数，所以要加1
                        strStartTime = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;

                        timeDialog = new TimePickerDialog(view.getContext(), new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                // TODO Auto-generated method stub
                                String time = hourOfDay + ":" + minute;
                                startTime.setText(strStartTime + " " + time);
                            }
                        }, hourOfDay, minute, true); // 最后一个参数设置是否为24小时制
                        timeDialog.show();
                    }
                }, year, monthOfYear, dayOfMonth); // 后面的三个参数对应于上面的年、月、日
                dateDialog.show();
            }
        });
        // 设置结束时间对话框
        endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                 * 实例化DatePickerDialog
                 */
                dateDialog = new DatePickerDialog(view.getContext(), new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker arg0, int year, int monthOfYear, int dayOfMonth) {
                        // 把获取的日期显示在文本框内，月份从0开始计数，所以要加1
                        strEndTime = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;

                        timeDialog = new TimePickerDialog(view.getContext(), new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                // TODO Auto-generated method stub
                                String time = hourOfDay + ":" + minute;
                                endTime.setText(strEndTime + " " + time);
                            }
                        }, hourOfDay, minute, true); // 最后一个参数设置是否为24小时制
                        timeDialog.show();
                    }
                }, year, monthOfYear, dayOfMonth); // 后面的三个参数对应于上面的年、月、日
                dateDialog.show();
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.appoint_btn_complete:
                Toast.makeText(this,"完成预约",Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_appoint_meeting, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //Back button
    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }


}
